using Core.SimpleTemp.Application;
using Core.SimpleTemp.Application.SysApp.DepartmentApp.Dtos;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Api.Api
{
    /// <summary>
    /// SysArea
    ///</summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class SysAreaController : CoreApiController
    {
        private readonly SysAreaService _service;
        private readonly FX_MapPositionService _mapPositionService;
        /// <summary>
        /// SysArea
        ///</summary>
        public SysAreaController(SysAreaService service, FX_MapPositionService mapPositionService)
        {
            _service = service;
            _mapPositionService = mapPositionService;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList")]
        [PermissionFilter(nameof(SysAreaController) + "_" + nameof(SysAreaController.GetList), AuthorizationConst.LIST)]
        //public async Task<IActionResult> GetList(PagingModel<SysAreaDto>  pagingQueryModel)
        public async Task<IActionResult> GetList(DepartmentPagingModel<SysAreaDto> pagingQueryModel)
        {
            pagingQueryModel.Limit = 999;
            IPageModel<SysAreaDto> result;
            result = await _service.GetList(pagingQueryModel);
            return this.JsonSuccess(result);
        }
        /// <summary>
        /// 根据当前登陆人所属工厂获取GetList
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetListSub")]
        [PermissionFilter(nameof(SysAreaController) + "_" + nameof(SysAreaController.GetListSub), AuthorizationConst.LIST)]
        //public async Task<IActionResult> GetList(PagingModel<SysAreaDto>  pagingQueryModel)
        public async Task<IActionResult> GetListSub(DepartmentPagingModel<SysAreaDto> pagingQueryModel)
        {
            pagingQueryModel.Model.SubFactory = UserIdentity.Factory;
            pagingQueryModel.Limit = 999;
            IPageModel<SysAreaDto> result;
            result = await _service.GetList(pagingQueryModel);
            return this.JsonSuccess(result);
        }
        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(SysAreaController) + "_" + nameof(SysAreaController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(SysAreaDto dto)
        {
            //添加
            if (Equals(dto.Id, Guid.Empty))
            {
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(SysAreaController) + "_" + nameof(SysAreaController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult>
            DeleteMutiAsync([FromBody]Guid[] ids)
        {

            await _service.DeleteBatchAsync(ids);
            return this.JsonSuccess();
        }


        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult>
Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);
            return this.JsonSuccess(model);
        }


        /// <summary>
        /// 导出
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("Export")]
        [PermissionFilter(nameof(SysAreaController) + "_" + nameof(SysAreaController.Export), AuthorizationConst.EXPORT)]
        public async Task<IActionResult>
            Export(PagingModel<SysAreaDto>
                pagingQueryModel)
        {
            var fileName = await _service.Export(pagingQueryModel);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }
        #endregion


        /// <summary>
        /// 获取风险分布图信息
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetMapInfo")]
        [PermissionFilter(nameof(SysAreaController) + "_" + nameof(SysAreaController.GetMapInfoAsync), "获取风险分布图信息")]
        public async Task<IActionResult> GetMapInfoAsync(Guid? ParentId)
        {
            var data = new
            {
                MapInfo = await _service.GetMapInfoAsync(ParentId),
                MapPosition = await _mapPositionService.GetAllAsync()
            };
            return this.JsonSuccess(data);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dtoList"></param>
        /// <returns></returns>

        [HttpPost("SaveList")]
        public async Task<IActionResult> SaveList([FromBody]List<SysAreaDto> dtoList)
        {
            foreach (var item in dtoList)
                if (!string.IsNullOrEmpty(item.Name))
                    await _service.InsertAsync(item);
            return this.JsonSuccess();
        }


        #region 根据工厂id查询
        /// <summary>
        /// 根据工厂id查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetListFactoryID")]

        public async Task<IActionResult> GetListFactoryID(DepartmentPagingModel<SysAreaDto> pagingQueryModel)
        {
            pagingQueryModel.Limit = 999;
            IPageModel<SysAreaDto> result;
            result = await _service.GetListFactoryID(pagingQueryModel);
            return this.JsonSuccess(result);
        }

        #endregion

    }
}
